#include "../inc/my_thread.h"
#include "../inc/bmp.h"

//全局变量 头文件声明 源文件定义
volatile int  rtb_should = 0;//等于1的时候，账号闪烁  等于2的时候，密码闪烁
int account_x = 400;
int password_x = 400;
volatile int monster_status = 1;//1为未被攻击 2为被攻击状态 3为死亡状态 -1为退出怪物线程
volatile int player_action = 1;//1为面向右边移动 2为面向左边移动 
int player_location = 0;//玩家位置
volatile int player_atk = 0;// 1为玩家攻击

//线程的函数 函数的返回值一定是void*  参数一定是void*
//账号闪烁的线程
void* real_time_bounce_1(void* arg)
{
	while(1)
	{
        if(rtb_should == 1){
            //pthread_testcancel();
            //实现闪烁
            if(account_x <= 720){
                printf("%d\n",account_x);
                bmp_show("./res/number_button/real_time1.bmp",account_x,240);
                usleep(500000);
                if(account_x <= 720 &&(rtb_should == 1||rtb_should == 2)){
                    bmp_show("./res/number_button/real_time2.bmp",account_x,240);
                    usleep(500000);
                }
            }
        }else{
            usleep(1000);
        }
	}
    //清理并退出
    if(rtb_should == -1){
        pthread_exit(NULL);
    }
}

//线程的函数 函数的返回值一定是void*  参数一定是void*
//密码闪烁的线程
void* real_time_bounce_2(void* arg)
{
	while(1)
	{
        if(rtb_should == 2){
            //pthread_testcancel();
            //实现闪烁
            if(password_x <= 720){
                bmp_show("./res/number_button/real_time1.bmp",password_x,340);
                usleep(500000);
                if(password_x <= 720 &&(rtb_should == 1||rtb_should == 2)){
                    bmp_show("./res/number_button/real_time2.bmp",password_x,340);
                    usleep(500000);
                }
            }
        }else{
            usleep(1000);
        }

	}
    //清理并退出
    if(rtb_should == -1){
        pthread_exit(NULL);
    }
}

//线程的函数 函数的返回值一定是void*  参数一定是void*
//怪物动态的线程
void* monster_dynamic(void* arg)
{
    //monster_status = 1;//怪物未被攻击
	while(1)
	{   
        //怪物首先是处于未被攻击状态
        if(monster_status == 1){
            bmp_show_small("./res/monster/unattacked1.bmp",690,300);
            usleep(500000);
            if(monster_status == 1){
                bmp_show_small("./res/monster/unattacked2.bmp",690,300);
                usleep(500000);
            }
        }
        //怪物首先是处于被攻击状态
        if(monster_status == 2){
            bmp_show_small("./res/monster/beattacked.bmp",690,300);
            usleep(500000);
            monster_status = 1;
        }
        if(monster_status == 3){
            bmp_show_small("./res/monster/dead1.bmp",690,300);
            usleep(250000);
            if(monster_status == 3){
                bmp_show_small("./res/monster/dead2.bmp",690,300);
                usleep(250000);
                if(monster_status == 3){
                    bmp_show_small("./res/monster/dead3.bmp",690,300);
                    usleep(250000);
                    if(monster_status == 3){
                        bmp_show_small("./res/monster/dead4.bmp",690,300);
                        usleep(250000);
                        if(monster_status == 3){
                            bmp_show_small("./res/monster/dead5.bmp",690,300);
                            usleep(250000);
                            if(monster_status == 3){
                                bmp_show_small("./res/monster/dead6.bmp",690,300);
                                usleep(250000);
                            }
                        }
                    }
                }
            }
        }
	}
    //清理并退出
    if(monster_status == -1){
        pthread_exit(NULL);
    }
}

//人物动态线程
void* player_dynamic(void* arg)
{
    //player_action = 1;//玩家面向右边移动
	while(1)
	{   
        //玩家首先面向右边
        if(player_action == 1){
            bmp_show_small("./res/npc/player_right_1.bmp",player_location,230);
            usleep(500000);
            if(player_action == 1){
                bmp_show_small("./res/npc/player_right_2.bmp",player_location,230);
                usleep(500000);
                if(player_action == 1){
                    bmp_show_small("./res/npc/player_right_3.bmp",player_location,230);
                    usleep(500000);
                    if(player_action == 1){
                        bmp_show_small("./res/npc/player_right_4.bmp",player_location,230);
                        usleep(500000);
                        if(player_action == 1){
                            bmp_show_small("./res/npc/player_right_5.bmp",player_location,230);
                            usleep(500000);
                            if(player_action == 1){
                                bmp_show_small("./res/npc/player_right_6.bmp",player_location,230);
                                usleep(500000);
                            }
                        }
                    }
                }
            }
        }
        //玩家面向左边
        if(player_action == 2){
            bmp_show_small("./res/npc/player_left_1.bmp",player_location,230);
            usleep(500000);
            if(player_action == 2){
                bmp_show_small("./res/npc/player_left_2.bmp",player_location,230);
                usleep(500000);
                if(player_action == 2){
                    bmp_show_small("./res/npc/player_left_3.bmp",player_location,230);
                    usleep(500000);
                    if(player_action == 2){
                        bmp_show_small("./res/npc/player_left_4.bmp",player_location,230);
                        usleep(500000);
                        if(player_action == 2){
                            bmp_show_small("./res/npc/player_left_5.bmp",player_location,230);
                            usleep(500000);
                            if(player_action == 2){
                                bmp_show_small("./res/npc/player_left_6.bmp",player_location,230);
                                usleep(500000);
                            }
                        }
                    }
                }
            }
        }
	}
    //清理并退出
    if(monster_status == -1){
        pthread_exit(NULL);
    }
}

//人物攻击线程
void* player_attack(void* arg)
{
    //默认不攻击
	while(1)
	{   
        //玩家攻击动画展示
        if(player_atk == 1){
            bmp_show_small("./res/attack/attack1.bmp",player_location + 80 ,280);
            usleep(200000);
            if(player_atk == 1){
                bmp_show_small("./res/attack/attack2.bmp",player_location + 80 ,280);
                usleep(200000);
                if(player_atk == 1){
                    bmp_show_small("./res/attack/attack3.bmp",player_location + 80 ,280);
                    usleep(200000);
                    if(player_atk == 1){
                        bmp_show_small("./res/attack/attack4.bmp",player_location + 80 ,280);
                        usleep(200000);
                    }
                }
            }
            player_atk = 0;//攻击完了就不进入循环了
        }else{
            //不攻击就线程暂停
            usleep(1000);
        }
	}
    //清理并退出
    if(player_atk == -1){
        pthread_exit(NULL);
    }
}
